<?php 
class Kriteria {
	var $arrPelamar;
	var $arrHasil;
	var $arrKriteriaA;
	var $arrKriteriaB;
	var $arrKriteriaC;
	var $arrKriteriaD;
	var $arrKriteriaE;
	var $arrKriteriaF;
	var $arrKriteriaG;
	var $dataH;
	var $dataI;
	var $dataJ;
	var $jmlData;
	var $CR;
	var $RI;
	var $DB;
	var $link;
	
	public $idTes;
	
	function Kriteria() {
		$this->arrPelamar = array();
		$this->arrHasil = array();
		$this->arrKriteriaA = array();
		$this->arrKriteriaB = array();
		$this->arrKriteriaC = array();
		$this->arrKriteriaD = array();
		$this->arrKriteriaE = array();
		$this->arrKriteriaF = array();
		$this->arrKriteriaG = array();
		$this->dataH = 0;
		$this->dataI = 0;
		$this->dataJ = 0;
		$this->CR = 0.1;
		$this->RI = array(0, 0.0, 0.0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49);
		$this->DB = 'kanisius';
	}
	
	function ABacaData($namaFile) {
		# baca data dari file dan dimasukkan ke array "arrKriteriaA" sebagai data awal
		$data = fopen($namaFile, 'r');
		$r = 0;
		
		#$file = file_get_contents('kriteria.csv');
		#print $file;
		
		while (!feof($data)) {
			$baris = fgets($data);
			$kolom = explode(",", $baris);
			$jmlKolom = count($kolom);
			
			if ($r <= $jmlKolom) {
				$this->arrKriteriaA[$r] = $kolom;
				$r++;
				#print "<br/> $baris <br/>";
			}
		}
		
		$this->jmlData = count($this->arrKriteriaA)-1;
		
		# simpan data pelamar
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$this->arrPelamar[$r] = $this->arrKriteriaA[$r][0];
		}
		
		print "==================== <br/>";
		print "=== OUTPUT SECTION A <br/>";
		print "==================== <br/>";
		var_dump($this->arrKriteriaA);
		fclose($data);
	}
	
	function BHitungBobot() {
		# perhitungan bobot dengan membagi setiap entri dengan jumlah total setiap kolom
		$arrTotKolom = array();
		
		# hitung total tiap kolom
		for ($c=1; $c <= $this->jmlData; $c++) {
			$totKolom = 0;
			
			for ($r=1; $r <= $this->jmlData; $r++) {
				$totKolom = $totKolom + $this->arrKriteriaA[$r][$c];
			}
			
			$arrTotKolom[$c] = round($totKolom, 3);
		}
		
		print "=== TOTAL PER KOLOM <br/>";
		var_dump($arrTotKolom);
		
		# hitung bobot, set presisi ke 3 digit
		for ($c=1; $c <= $this->jmlData; $c++) {
			for ($r=1; $r <= $this->jmlData; $r++) {
				$this->arrKriteriaB[$r][$c] = round($this->arrKriteriaA[$r][$c] / $arrTotKolom[$c], 3);
			}
		}
		
		print "==================== <br/>";
		print "=== OUTPUT SECTION B <br/>";
		print "==================== <br/>";
		var_dump($this->arrKriteriaB);
	}
	
	function CJumlahKanan() {
		for ($r=1; $r <= $this->jmlData; $r++) {
			$totBaris = 0;
			
			for ($c=1; $c <= $this->jmlData; $c++) {
				 $totBaris = $totBaris + $this->arrKriteriaB[$r][$c];
			}
			
			$this->arrKriteriaC[$r] = $totBaris;
		}
		
		print "==================== <br/>";
		print "=== OUTPUT SECTION C <br/>";
		print "==================== <br/>";
		var_dump($this->arrKriteriaC);
	}
	
	function DHitungVektor() {
		for ($r=1; $r <= $this->jmlData; $r++) {
			$this->arrKriteriaD[$r] = round($this->arrKriteriaC[$r] / $this->jmlData, 3);
		}
		
		print "==================== <br/>";
		print "=== OUTPUT SECTION D <br/>";
		print "==================== <br/>";
		var_dump($this->arrKriteriaD);
	}
	
	function EKonsistensi() {
		for ($r=1; $r <= $this->jmlData; $r++) {
			for ($c=1; $c <= $this->jmlData; $c++) {
				 $this->arrKriteriaE[$r][$c] = round($this->arrKriteriaA[$r][$c] * $this->arrKriteriaD[$c], 3);
			}
		}
		
		print "==================== <br/>";
		print "=== OUTPUT SECTION E <br/>";
		print "==================== <br/>";
		var_dump($this->arrKriteriaE);
	}
	
	function FJumlahKanan() {
		for ($r=1; $r <= $this->jmlData; $r++) {
			$totBaris = 0;
			
			for ($c=1; $c <= $this->jmlData; $c++) {
				 $totBaris = $totBaris + $this->arrKriteriaE[$r][$c];
			}
			
			$this->arrKriteriaF[$r] = $totBaris;
		}
		
		print "==================== <br/>";
		print "=== OUTPUT SECTION F <br/>";
		print "==================== <br/>";
		var_dump($this->arrKriteriaF);
	}
	
	function GBagiPrioritas() {
		for ($r=1; $r <= $this->jmlData; $r++) {
			$this->arrKriteriaG[$r] = round($this->arrKriteriaF[$r] / $this->arrKriteriaD[$r], 3);
		}
		
		print "==================== <br/>";
		print "=== OUTPUT SECTION G <br/>";
		print "==================== <br/>";
		var_dump($this->arrKriteriaG);
	}
	
	function HLambda() {
		$total = 0;
		
		for ($r=1; $r <= $this->jmlData; $r++) {
			$total = $total + $this->arrKriteriaG[$r];
		}
		
		$this->dataH = round($total / $this->jmlData, 3);
		print "==================== <br/>";
		print "=== OUTPUT SECTION H <br/>";
		print "==================== <br/>";
		var_dump($this->dataH);
	}
	
	function IHitungCI() {
		$this->dataI = round(($this->dataH - $this->jmlData) / ($this->jmlData - 1), 3);
		print "==================== <br/>";
		print "=== OUTPUT SECTION I <br/>";
		print "==================== <br/>";
		var_dump($this->dataI);
	}
	
	function JHitungCR() {
		$this->dataJ = round(($this->dataI / $this->RI[$this->jmlData]), 3);
		print "==================== <br/>";
		print "=== OUTPUT SECTION J <br/>";
		print "==================== <br/>";
		var_dump($this->dataJ);
	}
	
	function KCekCR() {
		print "==================== <br/>";
		print "=== OUTPUT SECTION K <br/>";
		print "==================== <br/>";
		
		if ($this->dataJ <= $this->CR) {
			print "Nilai CR = $this->dataJ berarti nilai CR <= $this->CR, maka konsistensi diterima";
		} else {
			print "Nilai CR = $this->dataJ berarti nilai CR > $this->CR, maka konsistensi tidak diterima";
		}
		
		print "<br/><br/>";
	}
	
	function KonekDB() {
		$this->link = mysql_connect('db4free.net:3306/kanisius', 'ajengbayu', 'bayuajeng'); 
		
		if (!$this->link) { 
			die('Autentikasi MySQL gagal: ' . mysql_error()); 
			return false;
		} else {
			$db_selected = mysql_select_db($this->DB, $this->link);
			
			if (!$db_selected) {
    		die("Tidak bisa membuat koneksi ke $this->DB: " . mysql_error());
			}
		}
	}
	
	function CekNamaTes() {
		$this->KonekDB();
		$query = sprintf("SELECT nmTes FROM tes WHERE idTes = '%s'", $this->idTes);
		$rs = mysql_query($query, $this->link);
		$data = mysql_fetch_array($rs);
		
		return $data['nmTes'];
	}
	
	function SimpanHasil() {
		$this->KonekDB(); 
				
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$this->arrHasil[$this->arrPelamar[$r]] = $this->arrKriteriaE[$r][$r];
			
			$query = sprintf("SELECT nama FROM nilaites WHERE idTes = %d AND LOWER(nama) = LOWER('%s')", 
				$this->idTes, $this->arrPelamar[$r]);
			$rs = mysql_query($query, $this->link);
			$data = mysql_fetch_array($rs);
			
			if (!empty($data)) {
				$query = sprintf("UPDATE nilaites SET nilai = %f WHERE idTes = %d AND LOWER(nama) = LOWER('%s')", 
					$this->arrKriteriaE[$r][$r], $this->idTes, $this->arrPelamar[$r]);
				$op = mysql_query($query, $this->link);
			} else {
				$query = sprintf("INSERT INTO nilaites VALUES ('%s', %d, %f)", 
					$this->arrPelamar[$r], $this->idTes, $this->arrKriteriaE[$r][$r]);
				$op = mysql_query($query, $this->link);
			}
		}
		
		print "==================== <br/>";
		print "=== OUTPUT SUMMARY   <br/>";
		print "==================== <br/>";
		var_dump($this->arrHasil);
		$namaTes = $this->CekNamaTes();
		
		print "Berhasil menyimpan hasil perhitungan TES $namaTes ke database $this->DB <br/>";
	}
	
	function SimpanKriteria() {
		$this->KonekDB();
		
		for ($r = 1; $r <= $this->jmlData; $r++) {
			$this->arrHasil[$this->arrPelamar[$r]] = $this->arrKriteriaE[$r][$r];
		
			$query = sprintf("UPDATE tes SET bobot = %f WHERE LOWER(nmTes) = LOWER('%s')", 
				$this->arrKriteriaE[$r][$r], $this->arrPelamar[$r]);
			$op = mysql_query($query, $this->link);
		}
		
		print "Berhasil menyimpan hasil perhitungan KRITERIA ke database $this->DB <br/>";
	}
	
	function HasilAkhir() {
		$this->KonekDB();
		
		print "==================== <br/>";
		print "=== NILAI PER TES    <br/>";
		print "==================== <br/>";
		$query = "SELECT n.nama, t.nmtes, (n.nilai * t.bobot) AS total 
			FROM nilaites n, tes t, pelamar p 
			WHERE n.idtes = t.idtes AND n.nama = p.nama";
		$rs = mysql_query($query, $this->link);
		
		while ($data = mysql_fetch_array($rs)) {
			print($data['nama'] . " - " . $data['nmtes'] . " : " . round($data['total'], 3) .'<br/>');
		};		
		
		print "<br/>";
		print "==================== <br/>";
		print "=== PERINGKAT        <br/>";
		print "==================== <br/>";
		$query = "SELECT n.nama, SUM(n.nilai * t.bobot) AS total 
			FROM nilaites n, tes t, pelamar p 
			WHERE n.idtes = t.idtes AND n.nama = p.nama
			GROUP BY n.nama
			ORDER BY total DESC";
		$rs = mysql_query($query, $this->link);
		$i = 0;
		
		while ($data = mysql_fetch_array($rs)) {
			$i++;
			print("PERINGKAT $i." . $data['nama'] . " : " . round($data['total'], 3) .'<br/>');
		};
	}
}
?> 